 #!/usr/bin/env python3
"""
特征重要性可视化API路由
提供特征重要性数据的API接口
"""

from fastapi import APIRouter, Depends, Query, HTTPException
from sqlalchemy.orm import Session
from typing import Dict, Any
import logging

from backend.config.database import get_db_session
from backend.service.feature_importance_service import FeatureImportanceService

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/feature-importance", tags=["特征重要性"])


@router.get("/visualization")
async def get_feature_importance_visualization(
    model_name: str = Query(..., description="模型名称"),
    target_date: str = Query(..., description="目标日期 (YYYY-MM-DD)"),
    db: Session = Depends(get_db_session)
):
    """获取特征重要性可视化数据"""
    try:
        service = FeatureImportanceService()
        visualization_data = service.get_feature_importance_visualization(model_name, target_date)
        
        return {
            "success": True,
            "data": visualization_data,
            "message": "特征重要性可视化数据获取成功"
        }
        
    except Exception as e:
        logger.error(f"获取特征重要性可视化数据失败: {e}")
        return {
            "success": False,
            "data": None,
            "message": f"获取特征重要性可视化数据失败: {str(e)}"
        }